require(["common","coAjax","queryString","Cookies","href"],function(jSouper,coAjax,QueryString,Cookies,href){
	/*
	 * 切换到注册状态
	 */
	$(function(){
		$("button[name='_to_sign_up']").click(function(){
			$('._company_info').addClass('focus');
			$('._bottom_top').addClass('focus');
		});
	})
	App.set("$Cache.login.username",Cookies.get("cache_user_login_name"));
    var queryString = new QueryString(location.hash);
	/*
	 * 注册
	 */
	;(function(){
	    var $username = $("input[name='u_name']");
	    var $phonenum = $("input[name='phone_num']");
	    var $email = $("input[name='e_mail']");
	    var $pwd = $("input[name='pwd']");
	    var $repwd = $("input[name='re_pwd']");
	    var $signUpButton = $("#sign_up_form input[type=submit]");
	    var $signUpForm = $("#sign_up_form form");
	    $signUpForm.on("submit",function (e) {
	    	e.preventDefault();   
	    });
	    $signUpButton.on("click",function (e) {
	    	var username = $username.val();
	    	var phonenum = $phonenum.val();
	    	var email = $email.val();
	    	var pwd = $pwd.val();
	    	var repwd = $repwd.val();
	    	coAjax.post(appConfig.user.sign_up_url,{
				name:username,
				password:pwd,
				confirm_password:repwd,
				mobile_phone:phonenum,
				email:email
	    	},function (result) {
	    		alert("success","注册成功");
	    		var $username = $("input[name='username']");
				var $password = $("input[name='password']");
				$username.val(username);
				$password.val(pwd);
				$("#to_in").click();
	    	},function (errorCode,xhr,errorMsg) {
	    		alert("error",errorMsg);
	    	});
	    });
	}());
	/*
	 *登录
	 */
    //获取一个验证码
    function loadCodeImg () {
    	// $code.val("");
    	// $signInImg.prop("src",appConfig.user.sign_in_img_code_url+"?_="+Math.random());
    };
    loadCodeImg();
	App.set("$Event.login.login",function (e) {
			var username = App.get("$Cache.login.username");
			var password = App.get("$Cache.login.password");
			var code = App.get("$Cache.login.code");
	    	coAjax.post(appConfig.user.sign_in_url,{
	    		name:username,
	    		password:password,
	    		code:code
	    	},function success(result) {
	    		alert("success","登录成功");
	    		App.set("loginer",result.result);
				Cookies.set("cache_user_login_name",username);
	    		var callbackUrl = queryString.get("cb_url");
    			href.jump("/#default/main");
	    	},function error (errorCode,xhr,errorMsg) {
	    		switch(errorCode){
	    			case "0001":
	    				alert("error","密码输入错误");
	    				break;
	    			case "00033":
	    				alert("error","“ "+username+" ”这个登录账号还没有注册，请检查您的用户名，或者先进行注册。");
	    				break;
	    			case "00034":
	    				alert("error","您还未输入登录用的用户名");
	    				break;
	    			case "00035":
	    				alert("error","您还未输入登录密码");
	    				break;
	    			case "00037":
	    				alert("error","您还未输入验证码");
	    				return;
	    				break;
	    			case "00038":
	    				alert("error","您输入验证码有些慢，为了您的安全，请重新输入");
	    				break;
	    			case "00039":
	    				alert("error","输入的验证码错误");
	    				break;
	    			default:
	    				alert("error",errorMsg);
	    		}
				loadCodeImg();
	    	});
	});
});
